package ui.popup
{
	import com.*;
	import flash.display.Shape;
	
	import events.*;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	
	import ui.*;
	import ui.button.GCButton;
	import ui.textfield.GCTextField;
	
	/**
	 * The popup that shows up if the player decides to turn in an
	 * NPC order
	 */
	public class GCNpcTurninPopup extends Sprite 
	{
		// Quantity of resource that is left to fulfill
		private var _quantityLeftInOrder:int;
		
		// Amount player has in their possession (either in ship or inventory)
		private var _quantityAvailable:int;
		
		// Name of resource
		private var _resourceRequested:String;
		
		private var _quantityAvailableText:GCTextField;
		private var _quantityScroll:GCNumberScroller;
		private var _giveMaxAmountButton:GCButton;
		private var _confirmButton:GCButton;
		private var _cancelButton:GCButton;

		private var _quantityToSupplyArea:Sprite;
		private var _quantityLeftArea:Sprite;
		private var _quantityLeftText:GCTextField;
		
		/*
		 * Show details of the order here!
		 */
		private var _orderDetails:GCTextField;
		
		/*
		 * Background 
		 */
		private var _backgroundImage:Sprite;
		
		public function GCNpcTurninPopup() 
		{
			_quantityScroll = new GCNumberScroller();
			_giveMaxAmountButton = new GCButton(onGiveMaxAmount);
			_confirmButton = new GCButton(onConfirm);
			_cancelButton = new GCButton(onCancel);
			
			_quantityAvailableText = new GCTextField();
			_quantityToSupplyArea = new Sprite();
			_quantityLeftArea = new Sprite();
			_quantityLeftText = new GCTextField();
			_orderDetails = new GCTextField();
			
			init();
		}
		
		public function openPopup(quantityLeft:int, quantityPossessed:int, resource:String, orderDetails:String):void
		{
			_orderDetails.setDimensions(this.width, 200);
			_orderDetails.setXY(0, -10);
			
			this._quantityLeftInOrder = quantityLeft;
			this._quantityAvailable = quantityPossessed;
			this._resourceRequested = resource;
			this._orderDetails.text = orderDetails;
			
			_quantityAvailableText.text = "Total you possess " + String(_quantityAvailable);
			_quantityLeftText.text = String(_quantityLeftInOrder) + " " + resource;
			_quantityScroll.setLimit(Math.min(_quantityAvailable, _quantityLeftInOrder));
		}
		
		public function closePopup():void
		{
			_quantityScroll.setValue(0);
		}
		
		public function getValue():uint
		{
			return _quantityScroll.value;
		}
		
		private function init():void
		{
			_quantityScroll.addEventListener(GCEvent.VALUE_CHANGED_EVENT, onValueChanged);
			
			var quantitySupplyLabel:GCTextField = new GCTextField();
			quantitySupplyLabel.text = "Amount to give: ";
			quantitySupplyLabel.wordWrap = true;
			quantitySupplyLabel.selectable = false;
			_quantityToSupplyArea.addChild(quantitySupplyLabel);
			_quantityToSupplyArea.addChild(_quantityScroll);
			_quantityToSupplyArea.y = 200;
			_quantityScroll.x = quantitySupplyLabel.x + quantitySupplyLabel.width;
			_quantityToSupplyArea.addChild(_quantityScroll);
			
			_quantityAvailableText.setXY(_quantityScroll.x + _quantityScroll.width, 0);
			_quantityToSupplyArea.addChild(_quantityAvailableText);
			
			var quantityLeftLabel:GCTextField = new GCTextField();
			quantityLeftLabel.text = "Amount customer still needs: ";
			quantityLeftLabel.wordWrap = true;
			quantityLeftLabel.selectable = false;
			_quantityLeftArea.x = _quantityToSupplyArea.x;
			_quantityLeftArea.y = _quantityToSupplyArea.y + _quantityToSupplyArea.height;
			_quantityLeftArea.addChild(quantityLeftLabel);
			_quantityLeftText.x = quantityLeftLabel.x + quantityLeftLabel.width;
			_quantityLeftArea.addChild(_quantityLeftText);
			
			_confirmButton.loadGraphic(GCResources.plate, 100, 30);
			_confirmButton.loadText("Give to Customer!");
			_confirmButton.y = _quantityLeftArea.y + _quantityLeftArea.height;
			
			_cancelButton.loadGraphic(GCResources.plate, 100, 30);
			_cancelButton.loadText("Cancel");
			_cancelButton.x = _confirmButton.x + _confirmButton.width + 10;
			_cancelButton.y = _confirmButton.y;
			
			_giveMaxAmountButton.loadGraphic(GCResources.plate, 100, 30);
			_giveMaxAmountButton.loadText("Give Max");
			_giveMaxAmountButton.x = _quantityAvailableText.x;
			_giveMaxAmountButton.y = _quantityAvailableText.y + 30;//_quantityAvailableText.height;
			
			//var bg:Bitmap = GCResources.getResource("genericBackground");
			//bg.height += this.height + 200;
			//this.addChild(bg);
			this.addChild(_orderDetails);
			this.addChild(_quantityToSupplyArea);
			this.addChild(_quantityLeftArea);
			this.addChild(_confirmButton);
			this.addChild(_cancelButton);
			_quantityToSupplyArea.addChild(_giveMaxAmountButton);
			
			//Popup background
			_backgroundImage = GCResources.getMaskedCanvasBackground(this.width, this.height);
			this.addChildAt(_backgroundImage, 0);
		}
		
		private function onConfirm(event:MouseEvent):void
		{
			dispatchEvent(new Event(GCEvent.CONTRIBUTED_NPC_ORDER));
		}
		
		private function onCancel(event:MouseEvent):void
		{
			dispatchEvent(new Event(GCEvent.POPUP_CLOSED_EVENT));
		}
		
		private function onGiveMaxAmount(event:MouseEvent):void
		{
			_quantityScroll.setValue((Math.min(_quantityAvailable, _quantityLeftInOrder)));
		}
		
		private function onValueChanged(event:Event):void
		{
			var newValue:int = _quantityScroll.value;
			_quantityLeftText.text = String(_quantityLeftInOrder - newValue) + " " + _resourceRequested;
		}
	}

}